package com.xinzhitong.www.controller;

import com.xinzhitong.www.enumeration.PostOrderType;
import com.xinzhitong.www.exception.enroll.MultipleEnrollException;
import com.xinzhitong.www.exception.positionget.NoPositionException;
import com.xinzhitong.www.exception.post.*;
import com.xinzhitong.www.exception.postcondition.NoPostConditionException;
import com.xinzhitong.www.exception.postgeneral.NoPostException;
import com.xinzhitong.www.service.PostService;
import com.xinzhitong.www.utils.Message;
import com.xinzhitong.www.utils.MyPageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 岗位控制器
 *
 * @author 徐鑫 魏骆淳
 */
@CrossOrigin
@RestController
@RequestMapping("post")
public class PostController {

    @Resource
    PostService postService;

    /**
     * 通过岗位id获取岗位信息
     *
     * @param id 岗位id
     * @return 岗位信息
     */
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Message info(@RequestParam("id") Integer id) {
        String msg;
        try {
            return Message.createSuc(postService.getPostInfo(id));
        } catch (PostNotExistException e) {
            e.printStackTrace();
            msg = "该岗位不存在";
        }
        return Message.createErr(msg);
    }

    /**
     * 报名指定岗位
     *
     * @param id 岗位id
     * @return 响应信息
     */
    @RequestMapping(value = "enrollThePost", method = RequestMethod.POST)
    public Message enrollThePost(@RequestParam("postId") Integer id) {
        String msg = "报名成功";
        try {
            postService.enrollThePost(id);
            return Message.createSuc(msg);
        } catch (PostNotExistException e) {
            e.printStackTrace();
            msg = "岗位信息已失效";
        } catch (MultipleEnrollException e) {
            e.printStackTrace();
            msg = "您已报名该岗位，请勿重复报名";
        }
        return Message.createErr(msg);
    }

    /**
     * 根据筛选条件获取岗位
     *
     * @param keyWord    关键字
     * @param conditions 条件合集
     *                   province 省
     *                   city 市
     *                   area 区
     *                   professionId 工种id
     *                   positionId 职位id
     *                   sexRequest 性别要求
     *                   minAgeRequest 最小年龄要求
     *                   maxAgeRequest 最大年龄要求
     *                   eduRequest 学历要求
     *                   minExpRequest 最小经验要求
     *                   maxExpRequest 最大经验要求
     * @return 符合筛选条件的岗位
     */
    @ResponseBody
    @RequestMapping(value = "postsSatisfyTheConditionsOf", method = RequestMethod.POST)
    public Message postsSatisfyTheConditionsOf(
            @RequestBody(required = false) Map<String, Object> conditions,
            @RequestParam(value = "keyWord", required = false, defaultValue = "") String keyWord,
            @RequestParam(value = "orderType", required = false, defaultValue = "NEW") PostOrderType postOrderType,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize
    ) {
        String msg;
        try {
            MyPageInfo posts =
                    postService.getPostsSatisfyTheConditionOf(conditions, keyWord, postOrderType, pageNo, pageSize);
            return Message.createSuc(posts);
        } catch (PostIsEmptyException e) {
            e.printStackTrace();
            msg = "暂无符合要求的岗位";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取学历要求枚举类
     *
     * @return 枚举列表
     */
    @RequestMapping(value = "eduEnum", method = RequestMethod.GET)
    public Message eduEnum() {
        return Message.createSuc(postService.getEduEnum());
    }

    /**
     * 获取招聘概览 可以关键字模糊搜索
     *
     * @param word     关键字
     * @param state    状态
     * @param pageNo   页码
     * @param pageSize 页大小
     * @return 招聘信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postGeneral", method = RequestMethod.GET)
    public Message postGeneral(
            @RequestParam(value = "word", required = false, defaultValue = "") String word,
            @RequestParam(value = "state", required = false, defaultValue = "") String state,
            @RequestParam("pageNo") int pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            return Message.createSuc(postService.getPostGeneral(state, word, pageNo, pageSize));
        } catch (NoPostException e) {
            e.printStackTrace();
            return Message.createErr("查询不到信息");
        }
    }

    /**
     * 获取招聘报名详情
     *
     * @param postId 招聘id
     * @return 报名详情
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postCondition", method = RequestMethod.GET)
    public Message postCondition(
            @RequestParam(value = "postId") Integer postId
    ) {
        try {
            return Message.createSuc(postService.getPostCondition(postId));
        } catch (NoPostConditionException e) {
            e.printStackTrace();
            return Message.createErr("此招聘暂无人报名");
        }
    }

    /**
     * 获取新报名人数
     *
     * @return 人数
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postNewEnroll", method = RequestMethod.GET)
    public Message postNewEnroll(
    ) {
        return Message.createSuc(postService.getPostNewEnroll());
    }

    /**
     * 下架招聘
     *
     * @param postId 招聘id
     * @return 下架是否成功
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "updatePost", method = RequestMethod.PUT)
    public Message updatePost(
            @RequestParam(value = "postId") Integer postId
    ) {
        return Message.createSuc(postService.underCarriage(postId));
    }

    /**
     * 删除招聘
     *
     * @param idList 招聘id列表
     * @return 成功删除信息
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "deletePost", method = RequestMethod.DELETE)
    public Message deletePost(
            @RequestBody List<Integer> idList
    ) {
        return Message.createSuc(postService.deletePost(idList));
    }

    /**
     * 工种id获取职位信息
     *
     * @param id 工种id
     * @return 职位信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "conditionIdGetPosition", method = RequestMethod.GET)
    public Message conditionIdGetPosition(
            @RequestParam(value = "id") Integer id
    ) {
        try {
            return Message.createSuc(postService.getPosition(id));
        } catch (NoPositionException e) {
            e.printStackTrace();
            return Message.createErr("此工种暂无职位");
        }
    }

    /**
     * 获取岗位基本信息
     *
     * @param postId 岗位id
     * @return 岗位基本信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postInformation", method = RequestMethod.GET)
    public Message getPostInformation(@RequestParam("postId") Integer postId) {
        String msg;
        try {
            return Message.createSuc(postService.getPostInformation(postId));
        } catch (NullPostInformationException e) {
            e.printStackTrace();
            msg = "岗位信息为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取岗位重要指标
     *
     * @param postId 岗位id
     * @return 岗位重要指标
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postIndex", method = RequestMethod.GET)
    public Message getPostIndex(@RequestParam("postId") Integer postId) {
        String msg;
        try {
            return Message.createSuc(postService.getPostIndex(postId));
        } catch (NullPostIndexException e) {
            e.printStackTrace();
            msg = "岗位重要指标为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取岗位基本信息和重要指标
     *
     * @param postId 岗位id
     * @return 岗位基本信息和重要指标
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postInformationAndIndex", method = RequestMethod.GET)
    public Message getPostInformationAndIndex(@RequestParam("postId") Integer postId) {
        String msg;
        try {
            Map<String, Object> postInformation = postService.getPostInformation(postId);
            Map<String, Object> postIndex = postService.getPostIndex(postId);
            postIndex.put("postName", postInformation.get("postName"));
            postIndex.put("companyName", postInformation.get("companyName"));
            postIndex.put("contact", postInformation.get("contact"));
            return Message.createSuc(postIndex);
        } catch (NullPostIndexException e) {
            e.printStackTrace();
            msg = "岗位重要指标为空";
        } catch (NullPostInformationException e) {
            e.printStackTrace();
            msg = "岗位信息为空";
        }
        return Message.createErr(msg);
    }

    /**
     * 发布新增招聘
     *
     * @param positionId           职位id
     * @param requirePopulation    要求人数
     * @param contact              联系方式
     * @param address              地址
     * @param baseSalary           基础工资
     * @param maxSalary            最大工资
     * @param minSalary            最小工资
     * @param paySalaryDate        发薪日期
     * @param payType              结算方式
     * @param salaryOtherNotice    工资其他说明
     * @param liveNotice           住宿说明
     * @param foodNotice           饮食说明
     * @param socialSecurityNotice 社保说明
     * @param liveFoodOtherNotice  其他说明
     * @param jobText              工作内容
     * @param jobTimeNotice        工作时间说明
     * @param jobWay               工作方式
     * @param jobOtherNotice       工作其他说明
     * @param sexRequest           性别要求
     * @param maxAgeRequest        最大年龄要求
     * @param minAgeRequest        最小年龄要求
     * @param eduBackground        学历要求
     * @param minExpRequest        最小经验要求
     * @param maxExpRequest        最大经验要求
     * @param credentialRequest    证书要求
     * @param employOtherNotice    雇佣其他说明
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "postRelease", method = RequestMethod.PUT)
    public Message releasePost(
            @RequestParam("positionId") Integer positionId,
            @RequestParam("requirePopulation") Integer requirePopulation,
            @RequestParam("contact") String contact,
            @RequestParam("address") String address,
            @RequestParam("baseSalary") double baseSalary,
            @RequestParam("minSalary") double minSalary,
            @RequestParam("maxSalary") double maxSalary,
            @RequestParam("paySalaryDate") Integer paySalaryDate,
            @RequestParam("payType") String payType,
            @RequestParam("salaryOtherNotice") String salaryOtherNotice,
            @RequestParam("liveNotice") String liveNotice,
            @RequestParam("foodNotice") String foodNotice,
            @RequestParam("socialSecurityNotice") String socialSecurityNotice,
            @RequestParam("liveFoodOtherNotice") String liveFoodOtherNotice,
            @RequestParam("jobText") String jobText,
            @RequestParam("jobTimeNotice") String jobTimeNotice,
            @RequestParam("jobWay") String jobWay,
            @RequestParam("jobOtherNotice") String jobOtherNotice,
            @RequestParam("sexRequest") Boolean sexRequest,
            @RequestParam("maxAgeRequest") Integer maxAgeRequest,
            @RequestParam("minAgeRequest") Integer minAgeRequest,
            @RequestParam("eduBackground") String eduBackground,
            @RequestParam("minExpRequest") Integer minExpRequest,
            @RequestParam("maxExpRequest") Integer maxExpRequest,
            @RequestParam("credentialRequest") String credentialRequest,
            @RequestParam("employOtherNotice") String employOtherNotice
    ) {
        postService.releasePost(
                positionId,
                requirePopulation,
                contact,
                address,
                baseSalary,
                maxSalary,
                minSalary,
                paySalaryDate,
                payType,
                salaryOtherNotice,
                liveNotice,
                foodNotice,
                socialSecurityNotice,
                liveFoodOtherNotice,
                jobText,
                jobTimeNotice,
                jobWay,
                jobOtherNotice,
                sexRequest,
                maxAgeRequest,
                minAgeRequest,
                eduBackground,
                minExpRequest,
                maxExpRequest,
                credentialRequest,
                employOtherNotice
        );
        String msg = "提交成功,待审核";
        return Message.createSuc(msg);
    }

    /**
     * 获取岗位完整信息
     *
     * @param postId 岗位id
     * @return 岗位完整信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postTotalInformation", method = RequestMethod.GET)
    public Message getPostTotalInformation(@RequestParam("postId") Integer postId) {
        String msg;
        try {
            return Message.createSuc(postService.getPostTotalInformation(postId));
        } catch (PostNotExistException e) {
            e.printStackTrace();
            msg = "岗位不存在";
        }
        return Message.createErr(msg);
    }

    /**
     * 编辑招聘
     *
     * @param postId               岗位id
     * @param positionId           职位id
     * @param requirePopulation    要求人数
     * @param contact              联系方式
     * @param address              地址
     * @param baseSalary           基础工资
     * @param maxSalary            最大工资
     * @param minSalary            最小工资
     * @param paySalaryDate        发薪日期
     * @param payType              结算方式
     * @param salaryOtherNotice    工资其他说明
     * @param liveNotice           住宿说明
     * @param foodNotice           饮食说明
     * @param socialSecurityNotice 社保说明
     * @param liveFoodOtherNotice  其他说明
     * @param jobText              工作内容
     * @param jobTimeNotice        工作时间说明
     * @param jobWay               工作方式
     * @param jobOtherNotice       工作其他说明
     * @param sexRequest           性别要求
     * @param maxAgeRequest        最大年龄要求
     * @param minAgeRequest        最小年龄要求
     * @param eduBackground        学历要求
     * @param minExpRequest        最小经验要求
     * @param maxExpRequest        最大经验要求
     * @param credentialRequest    证书要求
     * @param employOtherNotice    雇佣其他说明
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "postEdit", method = RequestMethod.PUT)
    public Message editPost(
            @RequestParam("postId") Integer postId,
            @RequestParam("positionId") Integer positionId,
            @RequestParam("requirePopulation") Integer requirePopulation,
            @RequestParam("contact") String contact,
            @RequestParam("address") String address,
            @RequestParam("baseSalary") double baseSalary,
            @RequestParam("minSalary") double minSalary,
            @RequestParam("maxSalary") double maxSalary,
            @RequestParam("paySalaryDate") Integer paySalaryDate,
            @RequestParam("payType") String payType,
            @RequestParam("salaryOtherNotice") String salaryOtherNotice,
            @RequestParam("liveNotice") String liveNotice,
            @RequestParam("foodNotice") String foodNotice,
            @RequestParam("socialSecurityNotice") String socialSecurityNotice,
            @RequestParam("liveFoodOtherNotice") String liveFoodOtherNotice,
            @RequestParam("jobText") String jobText,
            @RequestParam("jobTimeNotice") String jobTimeNotice,
            @RequestParam("jobWay") String jobWay,
            @RequestParam("jobOtherNotice") String jobOtherNotice,
            @RequestParam("sexRequest") Boolean sexRequest,
            @RequestParam("maxAgeRequest") Integer maxAgeRequest,
            @RequestParam("minAgeRequest") Integer minAgeRequest,
            @RequestParam("eduBackground") String eduBackground,
            @RequestParam("minExpRequest") Integer minExpRequest,
            @RequestParam("maxExpRequest") Integer maxExpRequest,
            @RequestParam("credentialRequest") String credentialRequest,
            @RequestParam("employOtherNotice") String employOtherNotice
    ) {
        postService.editPost(
                postId,
                positionId,
                requirePopulation,
                contact,
                address,
                baseSalary,
                maxSalary,
                minSalary,
                paySalaryDate,
                payType,
                salaryOtherNotice,
                liveNotice,
                foodNotice,
                socialSecurityNotice,
                liveFoodOtherNotice,
                jobText,
                jobTimeNotice,
                jobWay,
                jobOtherNotice,
                sexRequest,
                maxAgeRequest,
                minAgeRequest,
                eduBackground,
                minExpRequest,
                maxExpRequest,
                credentialRequest,
                employOtherNotice
        );
        String msg = "修改成功,待审核";
        return Message.createSuc(msg);
    }

    /**
     * 保存新增招聘
     *
     * @param map 招聘
     */
    @RequiresRoles(value = {"COMPANY"})
    @RequestMapping(value = "postSave", method = RequestMethod.POST)
    public Message savePost(@RequestBody Map<String, Object> map) {
        postService.savePost(map);
        String msg = "保存成功";
        return Message.createSuc(msg);
    }

    /**
     * 获取保存的招聘信息
     *
     * @return 招聘信息
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "postSaved", method = RequestMethod.GET)
    public Message getSavedPost() {
        try {
            return Message.createSuc(postService.getSavedPost());
        } catch (NullPostSavedException e) {
            return Message.createErr("未保存");
        }
    }

    /**
     * 获取最近编辑招聘
     *
     * @return 最近编辑招聘
     */
    @RequiresRoles(value = {"COMPANY", "ADMIN"}, logical = Logical.OR)
    @RequestMapping(value = "recentEditorPost", method = RequestMethod.GET)
    public Message getRecentEditorPost() {
        try {
            return Message.createSuc(postService.getRecentEditorPost());
        } catch (NullRecentEditorPost e) {
            return Message.createErr("无最近编辑招聘");
        }
    }

    /**
     * <<<<<<< HEAD
     * 审核招聘
     *
     * @return 招聘状态
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "postAudit", method = RequestMethod.GET)
    public Message auditPost(@RequestParam Integer postId) {
        String msg = "审核通过";
        try {
            postService.aduitPost(postId);
            return Message.createSuc(msg);
        } catch (PostNotExistException e) {
            e.printStackTrace();
            msg = "审核失败";
        }
        return Message.createErr(msg);
    }

    /**
     * 驳回招聘
     *
     * @return 招聘状态
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "postReject", method = RequestMethod.GET)
    public Message rejectPost(@RequestParam Integer postId) {
        String msg = "审核未通过";
        try {
            postService.rejectPost(postId);
            return Message.createSuc(msg);
        } catch (PostNotExistException e) {
            e.printStackTrace();
            msg = "审核失败";
        }
        return Message.createErr(msg);
    }

    /**
     * 获取招聘概览 可以关键字模糊搜索
     *
     * @param word         关键字
     * @param professionId 工种id
     * @param pageNo       页码
     * @param pageSize     页大小
     * @return 招聘信息
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "adminPostGeneral", method = RequestMethod.GET)
    public Message adminPostGeneral(
            @RequestParam(value = "word", required = false, defaultValue = "") String word,
            @RequestParam(value = "professionId", required = false, defaultValue = "") Integer professionId,
            @RequestParam("pageNo") Integer pageNo,
            @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize
    ) {
        try {
            return Message.createSuc(postService.getAdminPostGeneral(professionId, word, pageNo, pageSize));
        } catch (NoPostException e) {
            e.printStackTrace();
            return Message.createErr("查询不到信息");
        }
    }

    /**
     * 获取招聘指标
     *
     * @param postId 招聘id
     * @return 招聘报名人数
     */
    @RequiresRoles(value = {"ADMIN"})
    @RequestMapping(value = "adminPostEnrollCount", method = RequestMethod.GET)
    public Message adminPostEnrollCount(@RequestParam("postId") int postId) {
        return Message.createSuc(postService.getAdminPostEnrollCount(postId));
    }

}
